package com.tronsis.distribution.activity.me.stock;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.tronsis.distribution.R;
import com.tronsis.distribution.activity.me.agent.AgentExitActivity;
import com.tronsis.distribution.activity.me.order.DeliverDetailActivity;
import com.tronsis.distribution.activity.me.order.OrderDetailActivity;
import com.tronsis.distribution.bean.AskBuyBean;
import com.tronsis.distribution.bean.DeliveryBean;
import com.tronsis.distribution.bean.StockLogBean;
import com.tronsis.distribution.biz.factory.OrderBizFactory;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.adapter.recyclerview.CommonAdapter;
import com.tronsis.library.adapter.recyclerview.MultiItemTypeAdapter;
import com.tronsis.library.adapter.recyclerview.base.ViewHolder;
import com.tronsis.library.adapter.recyclerview.wrapper.LoadMoreWrapper;
import com.tronsis.library.http.ListBean;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.DateUtil;
import com.tronsis.library.util.DisplayUtil;
import com.tronsis.library.widget.MyDecoration;
import com.tronsis.library.widget.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class StockLogActivity extends ToolBarBaseActivity {

    @BindView(R.id.rv_stock_detail)
    RecyclerView mRvStockDetail;

    private LoadMoreWrapper mLoadMoreWrapper;
    private int mPage = 1;
    private int mSize = 15;
    private int mTotal;

    private List<StockLogBean> mStockLogList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stock_log);
        ButterKnife.bind(this);
        setTitle(true, getString(R.string.stock_detail));

        initRv();
        httpGetStockLog(mPage, mSize, false);
    }

    private void initRv() {
        CommonAdapter<StockLogBean> adapter = new CommonAdapter<StockLogBean>(mContext, mStockLogList, R.layout.item_stock_detail) {
            @Override
            protected void convert(ViewHolder holder, StockLogBean stockLogBean, int position) {
                holder.setText(R.id.tv_product_name, stockLogBean.getProductName());

                //BUY(进货), DELIVERY(发货), QUIT(退代理), TRANSFER(转移库存)
                String symbol = "";
                if (stockLogBean.getAmount() > 0) {
                    symbol = "+";
                }
                if (stockLogBean.getType() != null) {
                    // BUY(进货), DELIVERY(发货), QUIT(退代理), TRANSFER(转移库存)
                    switch (stockLogBean.getType()) {
                        case "BUY":
                            symbol = "+";
                            break;
                        case "DELIVERY":
                        case "QUIT":
                        case "TRANSFER":
                            if (stockLogBean.getAmount() > 0) {
                                symbol = "-";
                            }
                            break;
                    }
                }

                holder.setText(R.id.tv_amount, symbol + stockLogBean.getAmount());
                holder.setText(R.id.tv_time, DateUtil.getDateTimeString(stockLogBean.getCreatedOn()));

            }
        };

        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                StockLogBean stockLogBean = mStockLogList.get(position);
                if (stockLogBean != null) {
                    // BUY(进货), DELIVERY(发货), QUIT(退代理), TRANSFER(转移库存)
                    switch (stockLogBean.getType()) {
                        case "BUY":
                            Intent intentBuy = new Intent(mContext, OrderDetailActivity.class);
                            AskBuyBean askBuyBean = new AskBuyBean();
                            askBuyBean.setId(stockLogBean.getTypeId());
                            intentBuy.putExtra("AskBuyBean", askBuyBean);
                            intentBuy.setType("my");
                            startActivity(intentBuy);
                            break;
                        case "DELIVERY":
                            Intent intentDelivery = new Intent(mContext, DeliverDetailActivity.class);
                            DeliveryBean deliveryBean = new DeliveryBean();
                            deliveryBean.setId(stockLogBean.getTypeId());
                            intentDelivery.putExtra("DeliveryBean", deliveryBean);
                            startActivity(intentDelivery);
                            break;
                        case "QUIT":
                            Intent intentQuit = new Intent(mContext, AgentExitActivity.class);
                            startActivity(intentQuit);
                            break;
                        case "TRANSFER":
                            Intent intentTransfer = new Intent(mContext, OrderDetailActivity.class);
                            AskBuyBean bean = new AskBuyBean();
                            bean.setId(stockLogBean.getTypeId());
                            intentTransfer.putExtra("AskBuyBean", bean);
                            intentTransfer.setType("processing");
                            startActivity(intentTransfer);
                            break;
                        default:
                            break;
                    }
                }

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        mLoadMoreWrapper = new LoadMoreWrapper(adapter);
        mLoadMoreWrapper.setLoadMoreView(R.layout.rv_load_more);
        mLoadMoreWrapper.setOnLoadMoreListener(new LoadMoreWrapper.OnLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (mStockLogList.size() % 15 == 0 && mStockLogList.size() > 0 && mStockLogList.size() < mTotal) {
                    mPage = (mStockLogList.size() / 15) + 1;
                    httpGetStockLog(mPage, mSize, false);
                }
            }
        });
        int height = DisplayUtil.getInstance(mContext).dp2px(1);
        int color = Color.parseColor("#F4F4F4");
        RecycleViewDivider decoration = new RecycleViewDivider(mContext, MyDecoration.VERTICAL_LIST, height, color);
        mRvStockDetail.addItemDecoration(decoration);
        mRvStockDetail.setLayoutManager(new LinearLayoutManager(mContext));
        mRvStockDetail.setAdapter(mLoadMoreWrapper);
    }

    private void httpGetStockLog(int page, int size, final boolean isRefresh) {
        try {
            OrderBizFactory.createBiz().getStockLog(this, page, size, new UICallBack() {
                @Override
                public void onStart() {
                    if (isRefresh) {
                        showLoadingDialog();
                    }

                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();

                    ListBean listBean = (ListBean) data;
                    mTotal = listBean.getTotal();
                    List<StockLogBean> list = listBean.getList();
                    if (isRefresh) {
                        mStockLogList.clear();
                        mStockLogList.addAll(list);
                    } else {
                        mStockLogList.addAll(list);
                    }
                    mLoadMoreWrapper.notifyDataSetChanged();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            dismissLoadingDialog();
        }
    }
}
